﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication2.Models;

namespace MvcApplication2.Controllers
{
    public class HomeController : Controller
    {
        private Database1Entities5 db = new Database1Entities5();//соед с рел. бд
        private int pageSize = 4;


        public ActionResult Index(int pageNum = 0)
        {
            ViewData["PageNum"] = pageNum;
            ViewData["ItemsCount"] = db.Highereducations.Count();

            ViewData["PageSize"] = pageSize;

            var highereducations = (from highereducation in db.Highereducations orderby highereducation.name select highereducation).
                Skip(pageSize * pageNum).Take(pageSize).ToList();

            return View(highereducations);
        }


        public ActionResult ViewPage1()
        {
            return View();
        }

        public ActionResult ViewPage2()
        {
            return View();
        }
        public ActionResult About()
        {
            return View();
        }
        public ActionResult Details(int id)
        {
            var highereducationDetails = (from highereducation in db.Highereducations
                                          where highereducation.id == id
                                          select highereducation).First();//один элемент

            return View(highereducationDetails);
        }

        public ActionResult FacultyDetails(int id, int pageNum = 0)
        {
            ViewData["PageNum"] = pageNum;
            ViewData["ItemsCount"] = db.Facultys.Count();

            ViewData["PageSize"] = pageSize;
            var facultys = (from faculty in db.Facultys
                            where faculty.idhe == id
                            orderby faculty.name
                            select faculty). Skip(pageSize * pageNum).Take(pageSize).ToList();

            return View(facultys);
        }

        public ActionResult SpecialityDetails(int id, int pageNum = 0)
        {
            ViewData["PageNum"] = pageNum;
            ViewData["ItemsCount"] = db.Specialitys.Count();

            ViewData["PageSize"] = pageSize;
            var specialitys = (from speciality in db.Specialitys
                               where speciality.idfac == id
                               orderby speciality.name
                               select speciality).ToList();

            return View(specialitys);
        }
        public ActionResult ScoreDetails(int number, int pageNum = 0)
        {
            ViewData["PageNum"] = pageNum;
            ViewData["ItemsCount"] = db.Highereducations.Count();

            ViewData["PageSize"] = pageSize;
            var scores = (from score in db.Scores
                          where score.id_spec == number
                          orderby score.year
                          select score).ToList();

            return View(scores);
        }

        public ActionResult specSubDetails(int number)
        {
            List<MvcApplication2.Models.Subject> subjectsToView = new List<MvcApplication2.Models.Subject>();

            var specSubs = (from specSub in db.SpecSubs
                            where specSub.idspec == number
                            select specSub).ToList();


            foreach (SpecSub ss in specSubs)
            {
                var singleSubject = (from subject in db.Subjects
                                     where subject.id == ss.idsub
                                     select subject).SingleOrDefault();

                subjectsToView.Add(singleSubject);
            }

            return View(subjectsToView);
        }



        //Анкета
        public ActionResult Anketa()//отображение всех данных
        {
            var anketas = (from anketa in db.Anketas select anketa).ToList();

            return View(anketas);

        }

        //
        // GET: /Admin/Create

        public ActionResult CreateAnketa(int meow)//отображает представление для введения новых данных
        {
            Anketa anketa = new Anketa();
            anketa.id_special = meow;

            return View(anketa);
        }

        //
        // POST: /Admin/Create

        [HttpPost]

        public ActionResult CreateAnketa(Anketa anketa)//обработка запроса от пользователя
        {
            var highereducations = (from highereducation in db.Highereducations orderby highereducation.name select highereducation);
            try
            {
                if (ModelState.IsValid) //данные верны
                // TODO: Add insert logic here
                {
                    db.AddToAnketas(anketa);//добавление нового
                    db.SaveChanges();//сохранение изменений
                    return RedirectToAction("Index");
                }//
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(String.Empty, ex);//исключение
            }
            return View(anketa);
        }



        //public ActionResult TheUgly()
        //{
        //    var model = Data.GetFaculty();
        //    return View(model);
        //}

        //public static class Data
        //{
        //    public static Faculty GetFaculty()
        //    {
        //        return new Faculty { name = "ФИТ", id = 1 };
        //    }
        //}


        //protected void Button1_Click(object sender, EventArgs e)
        //{
        //    List<MvcApplication2.Models.Highereducation> subjectsToView = new List<MvcApplication2.Models.Highereducation>();
        //    foreach (ListItem item in ListBox1.Items)
        //    {
        //        if (item.Selected == true)
        //        {
        //            ListBox2.Items.Add(item);
        //        }
        //    }
        //}
        public ActionResult Vyborvuz()
        {
            var highereducations = (from highereducation in db.Highereducations select highereducation).ToList();

            return View(highereducations);
        }

        public ActionResult Vyborfac(int id)
        {
            var facultys = (from faculty in db.Facultys
                            where faculty.idhe == id
                            select faculty).ToList();

            return View(facultys);
        }


        public ActionResult Vyborspec(int id)
        {
            var specialitys = (from speciality in db.Specialitys
                               where speciality.idfac == id
                               select speciality).ToList();

            return View(specialitys);
        }

        

       
        //public ActionResult AddAnketa(int meow)
        //{

        //    return View();
        //}

       
    }
}



